package com.cip.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cip.admin.model.Institute;
import com.cip.admin.model.User;
import com.cip.admin.service.InstituteService;
import com.cip.common.util.CommonUtil;
import com.cip.common.util.SecurityUtil;
import com.cip.webapp.action.BaseAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import org.apache.velocity.app.VelocityEngine;
import org.springframework.ui.velocity.VelocityEngineUtils;

import net.sf.json.JSONObject;

public class GridAction extends BaseAction {
	
	public static final Log logger = LogFactory.getLog(com.cip.admin.action.GridAction.class);
	
	private InstituteService instituteService;
	private String gridName;
	
	public static String templateName = "GridTemplate.vm";
	
	
	public static String generateScript(VelocityEngine velocityEngine, Map<String, Object> context){
		  String result = null;
	        try {
	        	if(null == context.get("noOfRecords")){
	        		context.put("noOfRecords", "10");
	        	}
	        	if(null == context.get("link")){
	        		context.put("link", "");
	        	}
	            result = 
	                VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, context);
	        } catch (Exception e) {
	        	logger.error("Error while generating script using velocity",e);
	        }
		return result;
	}
	
	
	public void loadGridData() throws Exception{
		User loggedUser = SecurityUtil.getThreadLocalUser().get();
		try{
			JSONObject jsonobject = new JSONObject();
			System.out.println("grid name============>"+getGridName());
			List objArrayList = new ArrayList<Object>();
			if(getGridName().equalsIgnoreCase("institute")){
				objArrayList = loadInstData();
			}
			jsonobject.put("rows", objArrayList);	
			CommonUtil.generateResponseThroughJSON(getResponse(), jsonobject);
		}catch (Exception e) {
			logger.error(getText("errors.loading.grid")+getGridName()+e.getMessage()+" for the tenant "+loggedUser.getTenantId());
			addActionError(getText("errors.loading.grid")+e.getMessage());			
		}		
	}
	
	
	public List<Object> loadInstData() throws Exception{
		List<Institute> instList = instituteService.getAllInsList();
		List instArrayList = new ArrayList();
		for (Institute institute : instList) {
			Map instObj = new HashMap();
			String[] instArray = new String[4];
			instArray[0] = institute.getInstituteName();
			instArray[1] = institute.getPhoneNumber();
			instArray[2] = institute.getEmailId();
			instArray[3] = "<a href='/editInstitute?method=showEditInstitute&institute.id="+institute.getId()+"' style='text-decoration:underline;color:blue;'>View</a>";
			instObj.put("cell", instArray);
			instArrayList.add(instObj);			
		}
		return instArrayList;
	}

	public InstituteService getInstituteService() {
		return instituteService;
	}

	public void setInstituteService(InstituteService instituteService) {
		this.instituteService = instituteService;
	}

	public String getGridName() {
		return gridName;
	}

	public void setGridName(String gridName) {
		this.gridName = gridName;
	}

}
